package io.renren.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.dao.StuSubDao;
import io.renren.entity.StuSubEntity;
import io.renren.entity.SubjectEntity;
import io.renren.service.StuSubService;
import io.renren.service.SubjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


@Service("stuSubService")
public class StuSubServiceImpl extends ServiceImpl<StuSubDao, StuSubEntity> implements StuSubService {
    @Resource
    private SubjectService subjectService;

    /**
     * 查找学员_科目中间表
     *
     * @param subjectId
     * @param studentId
     * @return
     */
    @Override
    public StuSubEntity stuSub(Integer subjectId, Long studentId) {
        StuSubEntity stuSub = null;
        SubjectEntity subjectEntity = subjectService.selectById(subjectId);
        if (subjectEntity == null || subjectEntity.getSubjectType() != 2 && subjectEntity.getSubjectType() != 3) {
            return null;
        }
        EntityWrapper<StuSubEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("subject_id", subjectId);
        entityWrapper.eq("student_id", studentId);
        stuSub = this.selectOne(entityWrapper);
        //如果找不到中间表就新建一个
        if (stuSub == null) {
            stuSub = new StuSubEntity();
            stuSub.setSubjectId(subjectId);
            stuSub.setStudentId(studentId);
            stuSub.setCreateBy(studentId);
            stuSub.setCreateDate(new Date());
            insert(stuSub);
        }
        return stuSub;
    }
}
